package main;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Patrick
 */
public class LocationManager {
    private static LocationManager instance = null;
    private ArrayList<Location> locations = new ArrayList<Location>();
    private double bestDistance = 1.0 / 0.0, worstDistance = -1.0 / 0.0;
    private Location best, worst;
   
    public void create(int n) {
        Random generator = new Random();
        int x, y;
        
        for(int i = 1; i <= n; i++) {
            x = generator.nextInt(451);
            y = generator.nextInt(271);
            
            Location location = new Location(x,y,i);           
            this.locations.add(location);
        }        
    }
    
    double getLocationDistance(Location l1, Location l2) {
       return Math.sqrt(Math.pow(l1.getX() - l2.getX(), 2) + Math.pow(l1.getY() - l2.getY(), 2));
    }
    
    void findBestAndWorst() {
        double sum;
        
        for(int i = 0; i < this.locations.size(); i++) {
            sum = 0.0;
            
            for(int j = 0; j < this.locations.size(); j++) {
                if(i != j) {
                    sum += this.getLocationDistance(this.locations.get(i), this.locations.get(j));
                }
            }
            
            if(sum < this.bestDistance) {
                this.bestDistance = sum;
                this.best = locations.get(i);
            } 
            
            if (sum > this.worstDistance) {
                this.worstDistance = sum;
                this.worst = locations.get(i);
            }
        }
        
        this.best.setColor(Color.green);
        this.worst.setColor(Color.red);
    }
    
    protected LocationManager() {
        // Exists only to defeat instantiation.
    }
   
    public static LocationManager getInstance() {
      if(instance == null) {
         instance = new LocationManager();
      }
      
      return instance;
   }

    public void paint(Graphics g) {
        this.drawPoints(g);
        
        if(this.locations.size() <= 8){
            this.drawLines(g);
        }
    }

    protected void drawPoints(Graphics g) {
        for(Location location : this.locations) {
            location.draw(g);
        }
    }

    protected void drawLines(Graphics g) {
        for(int i = 0; i < this.locations.size(); i++) {
            for(int j = i+1; j < this.locations.size(); j++) {
                this.drawLine(g, this.locations.get(i), this.locations.get(j));
            }
        }
    }

    protected void drawLine(Graphics g, Location from, Location to) {
        g.drawLine((int)from.getX(), (int)from.getY(), (int)to.getX(), (int)to.getY());
    }
    
    public Location getBest() {
        return this.best;
    }
    
    public double getBestDistance() {
        return this.bestDistance;
    }
    
    public Location getWorst() {
        return this.worst;
    }
    
    public double getWorstDistance() {
        return this.worstDistance;
    }
}
